package com.example.springbootredis.controller;

import com.example.eshop.entity.Product;
import com.example.eshop.service.ProductService;
import com.example.eshop.dto.ProductDTO;
import com.example.eshop.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/product")
@Validated
public class ProductController {

    @Autowired
    private ProductService productService;

    // 1. 商品添加接口
    @PostMapping("/add")
    public Result addProduct(@Valid @RequestBody ProductDTO productDto) {
        productService.addProduct(productDto);
        return Result.success("商品添加成功");
    }

    // 2. 商品主图上传（七牛云）
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        String imageUrl = productService.uploadImage(file);
        return Result.success("上传成功", imageUrl);
    }

    // 4. 商品列表接口
    @GetMapping("/list")
    public Result getProductList() {
        List<Product> productList = productService.getOnShelfProducts();
        return Result.success(productList);
    }

    // 5. 商品详情接口
    @GetMapping("/detail/{productId}")
    public Result getProductDetail(@PathVariable Long productId) {
        Product product = productService.getProductDetail(productId);
        return Result.success(product);
    }
}